package com.carefree.system.modules.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carefree.system.common.utils.SecurityUtils;
import com.carefree.system.modules.admin.dao.DeptDao;
import com.carefree.system.modules.admin.entity.DeptEntity;
import com.carefree.system.modules.admin.service.DeptService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 组织机构
 *
 * @author tengbin
 */
@Service("deptService")
public class DeptServiceImpl extends ServiceImpl<DeptDao, DeptEntity> implements DeptService {

    @Resource
    private SecurityUtils securityUtils;

    /**
     * 查询部门信息并以树形结构构建
     *
     * @return
     */
    @Override
    public List<DeptEntity> getDeptWithTree() {
        List<DeptEntity> queryDeptList = list();
        return CollectionUtils.isEmpty(queryDeptList) ? null : buildTree(queryDeptList);
    }

    private List<DeptEntity> buildTree(List<DeptEntity> queryDeptList) {
        return queryDeptList.stream()
                .filter(dept -> dept.getParentId() == 0)
                .map(dept -> {
                    dept.setChildren(getChildrenList(dept, queryDeptList));
                    return dept;
                })
                .sorted((dept1, dept2) -> (dept1.getSortValue() == null ? 0 : dept1.getSortValue()) - (dept2.getSortValue() == null ? 0 : dept2.getSortValue()))
                .collect(Collectors.toList());
    }

    private List<DeptEntity> getChildrenList(DeptEntity rootDept, List<DeptEntity> queryDeptList) {
        return queryDeptList.stream()
                .filter(childrenDept -> childrenDept.getParentId().equals(rootDept.getId()))
                .map(childrenDept -> {
                    childrenDept.setParentName(rootDept.getName());
                    childrenDept.setChildren(getChildrenList(childrenDept, queryDeptList));
                    return childrenDept;
                })
                .sorted((childrenDept1, childrenDept2) -> (childrenDept1.getSortValue() == null ? 0 : childrenDept1.getSortValue()) - (childrenDept2.getSortValue() == null ? 0 : childrenDept2.getSortValue()))
                .collect(Collectors.toList());
    }

    /**
     * 根据id启用或禁用
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public boolean activeOrInActiveById(Long id, Integer status) {
        if (Objects.isNull(id) || Objects.isNull(status)) {
            return false;
        }
        DeptEntity deptEntity = getById(id);
        if (Objects.nonNull(deptEntity)) {
            deptEntity.setStatus(status);
            return updateById(deptEntity);
        }
        return false;
    }

    /**
     * 保存
     *
     * @param dept
     * @return
     */
    @Override
    public boolean saveDept(DeptEntity dept) {
        dept.setCreator(securityUtils.getPrincipalForUserId());
        return save(dept);
    }

    /**
     * 修改
     *
     * @param dept
     * @return
     */
    @Override
    public boolean updateDeptById(DeptEntity dept) {
        dept.setModifier(securityUtils.getPrincipalForUserId());
        return updateById(dept);
    }
}
